Public Class formselectvault

    ' The selected vault is stored here. If not selected (=cancel clicked), this is will be an empty string.
    Private m_szSelectedVault As String = ""

    ' Tells if the application can be exited.
    Private m_bAllowClose As Boolean = False

    ' Fires when the dialog is loaded.
    Private Sub formselectvault_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Button counter.
        Dim iButtonCount As Integer = 0

        Try

            ' Get vault connections.
            Dim oApp As New MFilesAPI.MFilesClientApplication
            Dim oVCs As MFilesAPI.VaultConnections = oApp.GetVaultConnections

            ' Check that a vault connection can be found.
            If (oVCs.Count = 0) Then
                MsgBox("No vault connections configured. Please contact your M-Files administrator.", MsgBoxStyle.Information)
            End If

            ' Loop all the connections.
            Dim oOneVC As MFilesAPI.VaultConnection
            For Each oOneVC In oVCs

                ' Create a new button.
                Dim btn As New Button
                btn.Size = New Size(200, 50)
                btn.Location = New Point(50, 20 + iButtonCount * 70)
                btn.UseVisualStyleBackColor = True
                btn.Text = oOneVC.Name
                btn.Name = "btnVault"
                'btn.Name = "btn_" & oOneVC.GetGUID

                ' Add button to the dialog.
                Me.Controls.Add(btn)

                ' Add event handler.
                AddHandler btn.Click, AddressOf DynamicClick

                ' Button counter.
                iButtonCount = iButtonCount + 1

            Next

        Catch ex As Exception

            ' Show possible error message.
            MsgBox(ex.Message, MsgBoxStyle.Information)

        End Try

        ' Create cancel button.
        Dim btnCancel As New Button
        btnCancel.Size = New Size(200, 50)
        btnCancel.Location = New Point(50, 20 + iButtonCount * 70)
        btnCancel.UseVisualStyleBackColor = True
        btnCancel.Text = "Cancel"
        btnCancel.Name = "btnCancel"

        ' Add button to the dialog.
        Me.Controls.Add(btnCancel)

        ' Add event handler.
        AddHandler btnCancel.Click, AddressOf DynamicClick

        ' Button counter.
        iButtonCount = iButtonCount + 1

        ' Set new height for the dialog.
        Me.Height = 50 + iButtonCount * 70

    End Sub

    ' Fires when the for is closing.
    Private Sub formselectvault_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        ' Check if the closing is allowed.
        If Not m_bAllowClose Then

            ' Cancel the close operation.
            e.Cancel = True

        End If

    End Sub

    ' A button clicked.
    Private Sub DynamicClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

        ' Check the button clicked.
        Dim btn As Button = CType(sender, Button)
        If (btn.Name = "btnCancel") Then
            m_szSelectedVault = ""
        Else
            m_szSelectedVault = btn.Text
        End If

        ' Close the dialog.
        m_bAllowClose = True
        Me.Close()

    End Sub

    ' Returns the selected vault. If cancel button clicked, the return value is an empty string.
    Public Function GetSelectedVault() As String
        Return m_szSelectedVault
    End Function

End Class